package org.skipper.common;

import org.skipper.common.constant.CodeInterface;
import org.skipper.common.constant.CommonCodeEnum;

/**
 * Created with IntelliJ IDEA.
 * Description: rpc返回封装
 *
 * @author shoujian
 * Date: 2020-06-03
 * Time: 1:35 下午
 */
public class RpcResult<T> extends Response {

    private static final long serialVersionUID = -2189378638499420057L;

    /**
     * 返回数据
     */
    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> RpcResult<T> createErrorMsg(String code, String message, T data) {
        RpcResult<T> rpcResult = new RpcResult<>();
        rpcResult.setCode(code);
        rpcResult.setMessage(message);
        rpcResult.setData(data);
        return rpcResult;
    }

    public static <T> RpcResult<T> createErrorMsg(CodeInterface code) {
        RpcResult<T> rpcResult = new RpcResult<>();
        rpcResult.setCode(code.getCode());
        rpcResult.setMessage(code.getMessage());
        return rpcResult;
    }

    public static <T> RpcResult<T> createErrorMsg(CodeInterface code, T data) {
        RpcResult<T> rpcResult = new RpcResult<>();
        rpcResult.setCode(code.getCode());
        rpcResult.setMessage(code.getMessage());
        rpcResult.setData(data);
        return rpcResult;
    }


    public static <T> RpcResult<T> createSuccessMsg(T data) {
        RpcResult<T> rpcResult = new RpcResult<>();
        rpcResult.setCode(CommonCodeEnum.SUCCESS.getCode());
        rpcResult.setMessage(CommonCodeEnum.SUCCESS.getMessage());
        rpcResult.setData(data);
        return rpcResult;
    }

    public static <T> RpcResult<T> createSuccessMsg() {
        RpcResult<T> rpcResult = new RpcResult<>();
        rpcResult.setCode(CommonCodeEnum.SUCCESS.getCode());
        return rpcResult;
    }

    public static boolean isResultSuccess(RpcResult rpcResult) {
        return rpcResult != null && CommonCodeEnum.SUCCESS.getCode().equals(rpcResult.getCode());
    }

    public static boolean isResultNotSuccess(RpcResult rpcResult) {
        return !isResultSuccess(rpcResult);
    }

    public static boolean isDataNotNull(RpcResult rpcResult) {
        return isResultSuccess(rpcResult) && rpcResult.getData() != null;
    }

    public static boolean isModelNull(RpcResult rpcResult) {
        return !isDataNotNull(rpcResult);
    }

    @Override
    public String toString() {
        return "RpcResult{" +
                "response =" + super.toString() +
                ", data=" + data +
                '}';
    }

}